import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-device-page',
	templateUrl: './device-page.component.html',
})
export class DevicePageComponent implements OnInit {
	validatorOptions = {
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			productTime: {
				validators: {
					notEmpty: {
						message: '请选择生产时间'
					}
				}
			},
			productNo: {
				validators: {
					notEmpty: {
						message: '请输入产品编号'
					}
				}
			},
			deviceId: {
				validators: {
					notEmpty: {
						message: '请输入设备编号'
					}
				}
			}
		}

	}
	page: any = {
		productTime: '',
		status: 0,
		productNo: '',
		productChannel: '',
		deviceId: '',
		iccid: '',
		productType: '',
		hasLIB: 0,
		gpsProducer: '',
		simProducer: '',
		mesNumber: '',
		resultStatus: '',
		sendTime: '',
		sendAddress: '',
		sendCompany: '',
		hwVersion: '',
		swVersion: '',
		blVersion: '',
		mbVersion: '',
		remake: '',
		inUser: '',
		outUser: '',
		creatTime: '',
		updateTime: '',
		id: '',
	};
	page_title: string = '新增';
	pageType: string = 'new';
	roleId = null;
	timer: any;
	curPage: number = 0;

	constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.roleId = queryParams.id;
			this.pageType = queryParams.type;
			this.curPage = queryParams.nowPage;
		});
	}
	//提交校验
	onSubmit() {
		if (this.pageType === 'new') {
			$('#roleForm').bootstrapValidator('validate'); //开启验证
			let isValid = $('#roleForm').data('bootstrapValidator').isValid();  //验证是否通过
			if (isValid) {
				this.updata();
			}
		} else if (this.pageType === 'updata') {
			$('#roleForm').bootstrapValidator('validate'); //开启验证
			this.timer = setTimeout(() => {
				let isValid = $('#roleForm').data('bootstrapValidator').isValid();  //验证是否通过
				if (isValid) {
					this.updata();
				}
			}, 200)
		}
	}
	//表单提交
	updata() {
		clearTimeout(this.timer);
		let reqdata = '';
		for (let keys in this.page) {
			reqdata = reqdata + keys + '=' + this.page[keys] + '&';
		}
		reqdata = reqdata.substring(0, reqdata.length - 1);
		let url = this.pageType === 'updata' ? 'device/edit.html' : 'device/addDeviceInfo.html'
		this.jsonApiService.http(url, reqdata).subscribe(data => {
			this.jsonApiService.message(data.message);
			if (data.success) {
				this.goBack();
			}
		})
	}
	//返回资源列表页
	goBack() {
		this.router.navigate(['/system/device-store'], { queryParams: { nowPage: this.curPage } });
	}
	ngOnInit() {
		this.initDate()
		if (this.pageType === 'updata') {
			this.page_title = '编辑';
			let reqdata = 'id=' + this.roleId;
			this.jsonApiService.http("device/deviceInfo.html", reqdata).subscribe(data => {
				if (data.success) {
					for (let item in this.page) {
						if (!data.data[item] && data.data[item] != 0) data.data[item] = '';
						this.page[item] = data.data[item];
					}
				} else {
					this.jsonApiService.message(data.message);
				}

			})
		} else {
			return
		}
	}
	//初始化日期
	initDate() {
		var that = this;
		System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
			() => {
				System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
					$("#productTime").datetimepicker({//时间插件
						format: "yyyy-mm-dd hh:ii:ss",
						language: "zh-CN",
						autoclose: true
					}).on("changeDate", function (e) {
						var date = e.date;
						if (!date) { return; }
						var time = common.getTimeMmss(date);
						that.page.productTime = time
						$("#roleForm").data('bootstrapValidator').resetForm();
					});
					$("#sendTime").datetimepicker({//时间插件
						format: "yyyy-mm-dd hh:ii:ss",
						language: "zh-CN",
						autoclose: true
					}).on("changeDate", function (e) {
						var date = e.date;
						if (!date) { return; }
						var time = common.getTimeMmss(date);
						that.page.sendTime = time
					});
					$("#creatTime").datetimepicker({//时间插件
						format: "yyyy-mm-dd hh:ii:ss",
						language: "zh-CN",
						autoclose: true
					}).on("changeDate", function (e) {
						var date = e.date;
						if (!date) { return; }
						var time = common.getTimeMmss(date);
						that.page.creatTime = time
					});
					$("#updateTime").datetimepicker({//时间插件
						format: "yyyy-mm-dd hh:ii:ss",
						language: "zh-CN",
						autoclose: true
					}).on("changeDate", function (e) {
						var date = e.date;
						if (!date) { return; }
						var time = common.getTimeMmss(date);
						that.page.updateTime = time
					});
				});
			});
	}

}